package com.dongjiantong.quarantine.model;

import com.dongjiantong.common.utils.JsonMapper;
import com.dongjiantong.mybatis.BaseEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import lombok.Data;
import org.hibernate.validator.constraints.NotEmpty;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 
 */
@Table(name="djt_butcher_admission_inspections")
@Data
public class DjtButcherAdmissionInspections extends BaseEntity implements Serializable {


    private static final ObjectMapper objectMapper = JsonMapper.nonEmptyMapper().getMapper();


    /**
     * 编号
     */
    @NotEmpty
    private String number;

    /**
     * 状态，0：未提交，1：已生效
     */
    private Byte status;

    /**
     * 操作人ID
     */
    private Long user_id;

    /**
     * 操作人名称
     */
    private String user_name;

    /**
     * 生产时间
     */
    private Date date;

    /**
     * 货主
     */
    private String owner;

    /**
     * 货主电话
     */
    private String owner_mobile;

    /**
     * 屠宰场id
     */
    private Integer butcher_farm_id;

    /**
     * 屠宰场名称
     */
    private String butcher_farm_name;

    /**
     * 无害化单据id
     */
    private Long harmless_id;

    /**
     * 《无害化处理通知单》编号
     */
    private String harmless_number;

    /**
     * 关联产地检疫单据内码
     */
    @NotEmpty
    private Long farm_report_id;

    /**
     * 关联产地检疫申报号
     */
    private String farm_report_number;

//    /**
//     * 关联屠宰检疫单id
//     */
//    private Long butcher_report_id;
//
//    /**
//     * 关联屠宰检疫单号
//     */
//    private String butcher_report_number;

    /**
     * 是否本地
     */
    private Integer is_local;

    /**
     * 屠宰场是否已选择,0:未选中;1:选中
     */
    private Integer butcher_selected;

    /**
     * 产品类型：牛、马、猪中
     */
    private Integer product_type;

    /**
     * 单位, 只、件
     */
    private Integer unit;

    /**
     * 数量
     */
    private Integer quantity;

    /**
     * 来源id
     */
    private Long source_id;

    /**
     * 来源名称
     */
    private String source_name;

    /**
     * 耳标号
     */
    @Transient
    private List<String> ear_tag_numbers;

    /**
     * 剩余耳标
     */
    @Transient
    private List<String> remainder_ears;

    /**
     * 临床检查
     */
    private Integer clinical_examination;

    /**
     * 是否佩戴规定的畜禽标识0:否;1:是;默认1
     */
    private Integer has_animal_identify;

    /**
     * 检疫证号
     */
    private String quarantine_cert_number;

    /**
     * 检疫证号照片
     */
    /**
     * 检疫合格证的图片
     */
    @Transient
    private List<String> quarantine_cert_photo_list;

    /**
     * 合格数
     */
    private Integer qualified_quantity;

    /**
     * 不合格数
     */
    private Integer unqualified_quantity;

    /**
     * 可用于屠宰的数量，即本单据的未屠宰数
     */
    private Integer remainder;

    /**
     * 已经屠宰的数量
     */
    private Integer slaughterings;

    /**
     * 扩展信息 瘦肉精检测抽样数,默认值为0 盐酸克伦特罗-阳性,默认值为0 莱克多巴胺-阳性,默认值为0 沙丁胺醇-阳性,默认值为0
     */
    @JsonIgnore
    private String extra_json;

    @Transient
    private Map<String , Object> extra;

    /**
     * 1：正常，-1：删除
     */
    private Integer delete_flag;


    private Date created_at;


    private Date updated_at;

    private static final long serialVersionUID = 1L;


    public void setExtra_json(String extra_json){
        this.extra_json = extra_json;

        if(Strings.isNullOrEmpty(extra_json)){
            this.extra = null;
        }else {
            try {
                this.extra = objectMapper.readValue(extra_json, new TypeReference<Map<String , Object>>() {});
            } catch (IOException e) {
                //ignore this fucking exception
            }
        }
    }


    public void setExtra(Map<String , Object> extra){

        this.extra = extra;

        if(extra == null || extra.isEmpty()){
            this.extra_json = null;
        }else {
            try {
                this.extra_json = objectMapper.writeValueAsString(extra);
            } catch (JsonProcessingException e) {
                //ignore this fucking exception
            }
        }

    }


}